Packages

Here we installed necessary packages

#install.packages("tidyverse")
library(ggplot2)                     
library(GGally)
library(tidyverse)
library(glmnet)
library(knitr)
library(dplyr)
library(tidyverse)
library(modelr)
library(pander)
library(corrplot)
library(readxl)
library(ISLR)
library(tidymodels)
library(ggthemes)
library(naniar)
library(ROCR)
library(maptree)
library(tree)
library(factoextra)
library(cluster)
library(randomForest)

Introduction

After a long nine to five at work, many people across the United States look to unwind with a bottle of beer and one of the world’s greatest pastimes, live sports! From October to May we watch basketball stars like Lebron James dropping dimes, or Stephen Curry splashing from threes, but what factors lead these players to such success in the basketball industry, and specifically what factors affect the salaries of NBA players, which can actually range from $377,645 all the way to $45,780,966. In our project we use statistical variables such as “PPG”, or “Offensive Rating” in different statistical machine learning models to predict the salaries of NBA players in the 2021-2022 season.

We apply several statistical machine learning models like logistic regression and clustering to the data in order to predict the salary of a player based on eight main predictors, carefully selected after several correlation calculations and observations. We plan to explore the benefits and detriments of each model, by calculating statistics such as mean squared error, area under the curve, etc. We hope you enjoy!

Before we dive into the data, the definitions of the variables should be clarified first. All the variables contained in our datasets will be listed below.

PPG: Points per game.

RPG: Rebounds per game.

APG: Assists per game.

SPG: Steals per game.

BPG: Blocks per game.

TPG: Turnovers per game.

MPG: Minutes per game.

Usage Rate: an estimate of the percentage of team plays used by a player while he was on the floor.

Free throw %: Free throw percentage.

Three-point %: Three point shot percentage.

Effective shooting %: a statistic that adjusts field goal percentage to account for the fact that three-point field goals count for three points while field goals only count for two points. Its goal is to show what field goal percentage a two-point shooter would have to shoot at to match the output of a player who also shoots three-pointers.

True shooting %: an advanced statistic that measures a player’s efficiency at shooting the ball. It is intended to more accurately calculate a player’s shooting than field goal percentage, free throw percentage, and three-point field goal percentage taken individually.

Versatility Index: measures a player’s ability to produce in more than one statistic. The metric uses points, assists, and rebounds. The average player will score around a five on the index, while top players score above 10. Calculated by: Versatility Index Formula=[(PPG)*(RPG)*APG)]^(0.333)

Offensive Rating: measures an individual player’s efficiency at producing points for the offense.

Defensive rating: measures an individual player’s efficiency at preventing the other team from scoring points.

Player Efficiency Rating: a method of determining a player’s impact on the game by measuring their per-minute performance. Rather than judging a player solely on their stats, their PER is a much more thorough performance indicator. It details a player and compares their value to that of other players in the league.

Win shares: a player statistic which attempts to divvy up credit for team success to the individuals on the team.

Box Plus Minus: estimates a basketball player’s contribution to the team when that player is on the court.

Value Over Replacement: estimates each player’s overall contribution to the team, measured vs. what a theoretical “replacement player” would provide, where the “replacement player” is defined as a player on minimum salary or not a normal member of a team’s rotation.

Read data

First, we read in the data that we downloaded. The first dataset is from a website called NBA stuffer, which contains many basketball statistics in an excel file. The second dataset is from kaggle and contains more basketball statistics, some different than the first dataset. The last dataset is from basketball-reference.com and lists out every players’ salary in the 2021-2022 season.

# NBA stuffer
X2020_2021_NBA_Stats_Player_Box_Score_Advanced_Metrics <- read_excel("2020-2021 NBA Stats  Player Box Score  Advanced Metrics.xlsx")
bball_stats <- as.data.frame(X2020_2021_NBA_Stats_Player_Box_Score_Advanced_Metrics)
my_colnames <- c('Rank', 'Player', 'Team', 'Position', 'Age', 'Games_Played', 'MPG', 'Minutes_percent', 'Usage_Rate' , 'Turnover_rate', 'free_throws_attempted', 'Free_throw_percent', '2-point_field goals_attempted', '2-point_percent', '3-point_field_goals_attempted', 'three_point_percent', 'effective_shooting_percent' , 'True_shooting_percent', 'PPG', 'RPG', 'Total_rebound_percent', 'APG', 'Assist_percent', 'SPG' ,'BPG', 'TPG', 'Versatility_Index', 'Offensive_Rating' , 'Defensive_Rating')
colnames(bball_stats) <- my_colnames
new_bball_stats <- bball_stats[-1,-1]

# kaggle
labels <- c('Player','Position', 'Age', 'Team', 'Games', 'Minutes_played', 'Player_Efficiency_Rating', 'true_shooting_percent', '3-point_attempt_rate', 'free-throw_attempt_rate', 'offensive_rebound_ percentage', 'defensive_rebound_percentage', 'total_rebound_percentage', 'assist_percentage', 'steal_percentage', 'block_percentage', 'turnover_percentage', 'usage_rate', 'offensive_win_shares', 'defensive_win_shares', 'win_shares','win_shares_per_48_minutes', 'Offensive_Box_Plus_Minus', 'Defensive_Box_Plus_Minus','Box_Plus_Minus','Value_Over_Replacement')
data_advanced <- read.csv("nba2021_advanced.csv", col.names = labels, na= "XXX")

# basektball-reference salaries
labels2 <- c('Rank', 'Player', 'Salary', 'Use', 'Guaranteed')
salaries <- read.csv("nba_salaries_21-22.csv", col.names = labels2)

Merge data into “master” dataset

This is where we merge the three datasets into one “master” dataset. This “master” dataset removes duplicated columns as well as columns that show unrelated/insignificant statistics. In addition, this “master” dataset changes all columns (except Player and Salary) to dbl for more convenient use later on. Small remark: a new column called Total_Minutes was added to see if it has any effect on data.

combine <- inner_join(new_bball_stats, data_advanced, by = 'Player') %>% 
  inner_join(salaries, by = 'Player')
  
selection <- subset(combine, select= -c(2:4,29:33,35:45,54,56:57)) 
less_data <- selection %>% relocate(c(PPG,RPG,APG,SPG,BPG,TPG), .before = MPG)

conversion <- less_data[,2:35] %>% mutate_if(is.character,as.numeric) %>% mutate(Total_Minutes = Games_Played*MPG)
conversion1 <- conversion %>% add_column(less_data$Player) 
names(conversion1)[names(conversion1) == "less_data$Player"] <- "Player"
conversion2 <- conversion1 %>% relocate((Player), .before = Games_Played)
conversion3 <- conversion2 %>% relocate((Total_Minutes), .before=MPG)

Filter data

Next, we wanted to filter out players who did not see the court often or were injured during the season. Keeping such players otherwise would have skewed the data and produced outliers. Here, players with total minutes less than 336 minutes and less than 7 agmes were taken out. (The entire season consisted of 72 games. 10% of 72 games is around 7 games. A full game is 48 minutes, so 7 full games is 336 minutes). In summary, a player had to play in 90% of the games to be considered. (The previous code was used to reduce columns, here we reduce rows).

reduced_data <- conversion3 %>% filter(Games_Played >= 7 & Total_Minutes >= 336) %>% select(Player:Salary) %>% drop_na()

Correlation to Salary

Since there are still a lot of predictors (columns) left, we need a way to only show the important ones. The rest can be omitted. Thus, we used a correlation plot and found correlation coefficients as related to salary.

my_cor <- reduced_data %>% select_if(is.numeric) %>% drop_na() %>% cor() %>% round(3) 
corrplot(my_cor, method = "circle", type = "upper", cex.pch=10)

salarycor <- reduced_data %>% select(Salary, Games_Played:Value_Over_Replacement) %>% drop_na()
cor(salarycor)[,"Salary"]
##                        Salary                  Games_Played 
##                       1.00000                       0.08676 
##                           PPG                           RPG 
##                       0.73988                       0.42568 
##                           APG                           SPG 
##                       0.62033                       0.41657 
##                           BPG                           TPG 
##                       0.17649                       0.64799 
##                 Total_Minutes                           MPG 
##                       0.45932                       0.67001 
##               Minutes_percent                    Usage_Rate 
##                       0.67008                       0.55813 
##                 Turnover_rate         free_throws_attempted 
##                      -0.02539                       0.63296 
##            Free_throw_percent 2-point_field goals_attempted 
##                       0.23258                       0.58382 
##               2-point_percent 3-point_field_goals_attempted 
##                       0.05285                       0.39700 
##           three_point_percent    effective_shooting_percent 
##                       0.12758                       0.11977 
##         True_shooting_percent         Total_rebound_percent 
##                       0.23653                       0.05951 
##                Assist_percent             Versatility_Index 
##                       0.46091                       0.58910 
##              Offensive_Rating              Defensive_Rating 
##                       0.22285                       0.15608 
##      Player_Efficiency_Rating          offensive_win_shares 
##                       0.54481                       0.55423 
##          defensive_win_shares                    win_shares 
##                       0.36287                       0.57743 
##     win_shares_per_48_minutes      Offensive_Box_Plus_Minus 
##                       0.36651                       0.62317 
##      Defensive_Box_Plus_Minus                Box_Plus_Minus 
##                      -0.04593                       0.54717 
##        Value_Over_Replacement 
##                       0.63365

Based from the correlation, we will filter those correlations with Salary above 0.6, meaning we will only use PPG, APG, MPG, TPG, Minutes_percent, free_throws_attempted, Offensive_Box_Plus_Minus, Value_Over_Replacement.

Final dataset and Histograms

This is where we put the final dataset. It has our players, their salaries, and the 8 predictors we want. We will be referring to our_data the rest of the project. As a start to visualize the data, we plotted the response variable Salary using a histogram and the scatter plots of Salary vs. the 8 predictors which has the greatest correlation with salary.

our_data <- reduced_data %>% select(Player, Salary, PPG, APG, MPG, TPG, MPG, Minutes_percent, free_throws_attempted, Offensive_Box_Plus_Minus, Value_Over_Replacement)

hist(our_data$Salary, main = "Histogram of NBA salaries 2021-2022", xlab="Salary Amount",breaks = "Sturges", labels = TRUE)

plot(our_data$Salary, our_data$PPG, main="Scatterplot of Salary vs. PPG",
   xlab="Salary", ylab="PPG", pch=19)

plot(our_data$Salary, our_data$APG, main="Scatterplot of Salary vs. APG",
   xlab="Salary", ylab="APG", pch=19)

plot(our_data$Salary, our_data$MPG, main="Scatterplot of Salary vs. MPG",
   xlab="Salary", ylab="MPG", pch=19)

plot(our_data$Salary, our_data$TPG, main="Scatterplot of Salary vs. TPG",
   xlab="Salary", ylab="TPG", pch=19)

plot(our_data$Salary, our_data$Minutes_percent, main="Scatterplot of Salary vs. Minutes Percent",
   xlab="Salary", ylab="Minutes Percent", pch=19)

plot(our_data$Salary, our_data$free_throws_attempted, main="Scatterplot of Salary vs. Free Throws   Attempted",
   xlab="Salary", ylab="Free Throws Attempted", pch=19)

plot(our_data$Salary, our_data$Offensive_Box_Plus_Minus, main="Scatterplot of Salary vs. Offensive Box Plus Minus",
   xlab="Salary", ylab="Offensive Box Plus Minus", pch=19)

plot(our_data$Salary, our_data$Value_Over_Replacement, main="Scatterplot of Salary vs. Value Over Replacement",
   xlab="Salary", ylab="Value Over Replacement", pch=19)

Training and Testing data

In our models we need testing and training datas. Training data will be 80% and testing will be 20%. Here we define them and will proceed with these in mind.

set.seed(3112022)
fit_data <- our_data[-1] 
new_data <- resample_partition(fit_data, p = c(test=0.2, train=0.8)) 
training <- as.data.frame(new_data$train)
testing <- as.data.frame(new_data$test)

Linear regression

Our first model is linear regression. There were a total of 3 fitted models. The first model consists of all 9 predictors. Based from the summary of that first fit, the second fit contains only predictors that were significant. Since the first and second fit were closely aligned, the third fit looked at the summary of the first fit and chose more signicant predictors than the second fit. At the end of each fit, I calculated MSE for that respective fit.

fit1 <- lm(Salary ~ PPG + APG + MPG + TPG + Minutes_percent + free_throws_attempted + Offensive_Box_Plus_Minus + Value_Over_Replacement, data = training)
summary(fit1)
## 
## Call:
## lm(formula = Salary ~ PPG + APG + MPG + TPG + Minutes_percent + 
##     free_throws_attempted + Offensive_Box_Plus_Minus + Value_Over_Replacement, 
##     data = training)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -19238393  -3666432   -320330   2562992  25441992 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)   
## (Intercept)              -5323369    1674227   -3.18   0.0016 **
## PPG                        454929     179858    2.53   0.0119 * 
## APG                       1107092     355792    3.11   0.0020 **
## MPG                      -7948717   10760389   -0.74   0.4607   
## TPG                        191955    1128868    0.17   0.8651   
## Minutes_percent           3918259    5168273    0.76   0.4490   
## free_throws_attempted        8874       6805    1.30   0.1932   
## Offensive_Box_Plus_Minus   193023     263729    0.73   0.4648   
## Value_Over_Replacement    3707858    1405745    2.64   0.0088 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6300000 on 300 degrees of freedom
## Multiple R-squared:  0.618,  Adjusted R-squared:  0.608 
## F-statistic: 60.7 on 8 and 300 DF,  p-value: <2e-16
train.predict1 <- predict(fit1, training)
test.predict1 <- predict(fit1, testing)
mean((train.predict1-training$Salary)^2)
## [1] 3.859e+13
mean((test.predict1-testing$Salary)^2)
## [1] 5.39e+13
fit2 <- lm(Salary ~ PPG + APG + Value_Over_Replacement, data= training)
summary(fit2)
## 
## Call:
## lm(formula = Salary ~ PPG + APG + Value_Over_Replacement, data = training)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -18291447  -3697069   -371688   2935890  25291063 
## 
## Coefficients:
##                        Estimate Std. Error t value Pr(>|t|)    
## (Intercept)            -3240037     857052   -3.78  0.00019 ***
## PPG                      782251      92103    8.49  9.0e-16 ***
## APG                     1237489     248849    4.97  1.1e-06 ***
## Value_Over_Replacement  4376932    1020652    4.29  2.4e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6320000 on 305 degrees of freedom
## Multiple R-squared:  0.61,   Adjusted R-squared:  0.606 
## F-statistic:  159 on 3 and 305 DF,  p-value: <2e-16
train.predict2 <- predict(fit2, training)
test.predict2 <- predict(fit2, testing)
mean((train.predict2-training$Salary)^2)
## [1] 3.94e+13
mean((test.predict2-testing$Salary)^2)
## [1] 5.555e+13
fit3 <- lm(Salary ~ APG + Value_Over_Replacement, data= fit_data)
summary(fit3)
## 
## Call:
## lm(formula = Salary ~ APG + Value_Over_Replacement, data = fit_data)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -26880280  -4606166  -1323534   3243761  26048134 
## 
## Coefficients:
##                        Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             2075769     619850    3.35  0.00089 ***
## APG                     2164600     214602   10.09  < 2e-16 ***
## Value_Over_Replacement  9131247     850494   10.74  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 7200000 on 383 degrees of freedom
## Multiple R-squared:  0.527,  Adjusted R-squared:  0.525 
## F-statistic:  213 on 2 and 383 DF,  p-value: <2e-16
train.predict3 <- predict(fit3, training)
test.predict3 <- predict(fit3, testing)
mean((train.predict3-training$Salary)^2)
## [1] 4.873e+13
mean((test.predict3-testing$Salary)^2)
## [1] 6.215e+13

Based from the 3 fitted models, I chose the model with the least MSE for training and testing. The first model had the least MSE for both training and testing so I will use fit1. This makes sense as the largest R-squared was fit1.

Logistic regression

Since all the nine variables we are going to use are numeric variables, and the logistic regression cannot be used to analyze numeric variables, we will create a new predictor called “salarygreater”. “Salarygreater” tests whether a player’s salary is greater than the average salary of the league. By producing this new binomial predictor, we can use logistic regression to analyze the salary condition of the players in a new perspective.

First, we will calculate the average salary of all players, and create the new binary variable “salarygreater” in the training dataset to test whether a certain player’s salary is greater than the average. If the player’s salary is greater than the average, it will show “1” in the “salarygreater”; if not, it will show “0”.

mean_train_salary <- mean(training$Salary)
training_salarymean<- training %>% mutate(salarygreater=factor(ifelse(Salary >= mean_train_salary, 1, 0), levels=c(0, 1)))

Next, we will fit a logistic regression on the “salarygreater” in the training dataset. After that, we will predict based on the “majority rule”: if the predicted probability is greater than 0.5, classify the observation as 1, otherwise, classify it as 0.

training_logit <- glm(salarygreater ~ PPG + APG + MPG + TPG + Minutes_percent + free_throws_attempted + Offensive_Box_Plus_Minus + Value_Over_Replacement, data = training_salarymean, family = 'binomial')

salarymean_pred_training <- predict(training_logit, training_salarymean, type="response")
train_maj_rule <- ifelse(salarymean_pred_training > 0.5, 1,0)

After that, we will define a function called “calc_error_rate” and use it to calculate the error rate of the of the logistic model on the training dataset, which is created above.

calc_error_rate <- function(predicted.value, true.value){
  return(mean(true.value!=predicted.value))}
calc_error_rate(train_maj_rule, training_salarymean$salarygreater)
## [1] 0.1942

Similary, we will do the same process on the testing dataset to predict based on the logistic model created above and calculate the test error.

testing_salarymean<- testing %>% mutate(salarygreater=factor(ifelse(Salary >= mean_train_salary, 1, 0), levels=c(0, 1)))
salarymean_pred_testing <- predict(training_logit,  testing_salarymean, type="response")
test_maj_rule2 <- ifelse(salarymean_pred_testing > 0.5, 1,0)
calc_error_rate(test_maj_rule2, testing_salarymean$salarygreater)
## [1] 0.1558

Finally, we can plot an ROC curve and calculate the area under the curve (AUC) for the test data to see the performance of the logistic regression model.

pred <- prediction(salarymean_pred_testing, testing_salarymean$salarygreater)
perf <- performance(pred, measure = "tpr", x.measure = "fpr")
plot(perf, col = 2, lwd = 3, main = "ROC curve")
abline(0, 1)

AUC is shown below.

auc <- performance(pred, "auc")@y.values[[1]]
auc
## [1] 0.9184

Training, Testing for Ridge/ Lasso

x <- model.matrix(Salary~., fit_data)
y <- fit_data$Salary

x.train <- as.matrix(training[,-1])
y.train <- as.matrix(training$Salary)
x.test <- as.matrix(testing[,-1])
y.test <- as.matrix(testing$Salary)

Ridge / Lasso

# ridge
lambda.list.ridge = 1000 * exp(seq(0, log(1e-5), length = 100))

ridge.mod = cv.glmnet(x.train, y.train, alpha=0,lambda=lambda.list.ridge, nfolds=5)

ridge.pred_1=predict(ridge.mod, s = ridge.mod$lambda.min, type="coefficients", newx=x.test)

ridge.pred=predict(ridge.mod, s = ridge.mod$lambda.min, newx=x.test)
mean((ridge.pred-y.test)^2)
## [1] 5.408e+13
# cross-validation to choose best tuning parameter
set.seed(3142022)
cv.out.ridge = cv.glmnet(x.train, y.train, alpha= 0)
plot(cv.out.ridge)
abline(v=log(cv.out.ridge$lambda.min), col = "blue", lwd=3, lty=2)

bestlam = cv.out.ridge$lambda.min
bestlam
## [1] 3010560
ridge.pred=predict(ridge.mod, s = bestlam, newx=x.test)
mean((ridge.pred-y.test)^2)
## [1] 5.408e+13
out = glmnet(x,y,alpha=0)
predict(out, type="coefficients", s=bestlam)
## 10 x 1 sparse Matrix of class "dgCMatrix"
##                                s1
## (Intercept)              -4562064
## (Intercept)                     .
## PPG                        241527
## APG                        758927
## MPG                        146306
## TPG                        843915
## Minutes_percent             71181
## free_throws_attempted        9386
## Offensive_Box_Plus_Minus   425212
## Value_Over_Replacement    2959665
# lasso
set.seed(3142022)
lambda.list.lasso = 2 * exp(seq(0, log(1e-4), length = 100))

lasso.mod <- glmnet(x.train, y.train, alpha=1, lambda=lambda.list.lasso, nfolds = 5)

plot(lasso.mod, xvar="lambda", label=TRUE)

cv.out.lasso = cv.glmnet(x.train,y.train,alpha=1)
plot(cv.out.lasso)
abline(v=log(cv.out.lasso$lambda.min), col="red", lwd=3, lty=2)

bestlam2 = cv.out.lasso$lambda.min
lasso.pred = predict(lasso.mod, s = bestlam2, newx = x.test)
mean((lasso.pred-y.test)^2)
## [1] 5.408e+13
out = glmnet(x, y, alpha=1, lambda=lambda.list.lasso)
lasso.coef = predict(out, type="coefficients", s=bestlam)
lasso.coef
## 10 x 1 sparse Matrix of class "dgCMatrix"
##                                s1
## (Intercept)              -5063779
## (Intercept)                     .
## PPG                        469084
## APG                       1266036
## MPG                        160656
## TPG                       -882401
## Minutes_percent             39006
## free_throws_attempted        8341
## Offensive_Box_Plus_Minus   225082
## Value_Over_Replacement    3852047

Regression Decision Tree:

Here, we start by using the tree() function to create a decision tree using our_data. Then we take a summary of this tree to see variables used in tree construction, number of terminal nodes, and residual information.

tree_our_data=tree(Salary~. , data = our_data) 
summary(tree_our_data)
## 
## Regression tree:
## tree(formula = Salary ~ ., data = our_data)
## Variables actually used in tree construction:
## [1] "PPG"                      "Minutes_percent"         
## [3] "MPG"                      "TPG"                     
## [5] "free_throws_attempted"    "Value_Over_Replacement"  
## [7] "Offensive_Box_Plus_Minus"
## Number of terminal nodes:  9 
## Residual mean deviance:  3.44e+13 = 1.3e+16 / 377 
## Distribution of residuals:
##      Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
## -29300000  -2560000   -652000         0   2050000  24500000

Here we plot our decision tree using two different tree plotting functions, plot and draw tree. Both trees have 7 terminal nodes.

# plot the fitted tree
plot(tree_our_data)
text(tree_our_data, pretty = 0, cex = .4, col = "blue")
title("Decision tree on our_data", cex = 0.8)

draw.tree(tree_our_data, nodeinfo=TRUE, cex = 0.4)

Furthermore, we fit the regression decision tree model to the training set and plot the tree using the draw.tree() command.

# Fit model on training set
tree.nba = tree(Salary~. , data = training)
 
# Plot the tree
draw.tree(tree.nba, nodeinfo=TRUE, cex = 0.4)
title("Regression Tree Built on Training Set")

We can see in the summary that the tree modeled by the training set has 10 terminal nodes, whereas the previous tree had 12 terminal nodes.

summary(tree.nba)
## 
## Regression tree:
## tree(formula = Salary ~ ., data = training)
## Variables actually used in tree construction:
## [1] "free_throws_attempted"    "MPG"                     
## [3] "Minutes_percent"          "TPG"                     
## [5] "Offensive_Box_Plus_Minus" "PPG"                     
## Number of terminal nodes:  8 
## Residual mean deviance:  3.31e+13 = 9.96e+15 / 301 
## Distribution of residuals:
##      Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
## -19800000  -2570000   -771000         0   2190000  25600000

Now here we do a prediction on the test set using the predict() command. We specify type=‘vector’ because we are working with a regression decision tree. Then we calculate the mean squared error which is 6.68e+13 and root mean squared error which is 8173153.

# Predict on test set
tree.pred = predict(tree.nba, testing, type="vector")
tree.pred
##        3        8        9       14       25       37       44       49 
## 16991504 16991504 16991504  7816237 15205071  3160422  3160422 26165424 
##       50       63       65       71       76       82       84       98 
## 16991504  3160422 26165424  3160422 15205071  3160422  9271695  7816237 
##      103      109      112      115      120      121      129      135 
## 26165424  3160422 26165424  6761423 26165424 16991504  3160422  6761423 
##      141      142      145      148      163      164      167      175 
## 39076097 39076097 16991504  9271695  3160422  9271695  9271695 16991504 
##      182      183      196      199      200      201      204      205 
## 16991504  3160422  7816237  9271695  3160422  3160422 26165424  3160422 
##      210      213      214      220      226      231      240      246 
##  3160422  3160422 39076097 15205071  3160422  9271695  3160422 26165424 
##      262      270      275      277      278      283      285      296 
##  3160422  3160422 15205071  3160422  3160422  3160422 15205071  6761423 
##      297      300      304      305      308      324      326      328 
##  3160422  9271695  9271695  9271695  3160422  6761423 16991504 15205071 
##      329      337      362      364      365      366      368      369 
##  3160422  7816237  3160422  3160422  7816237  3160422 15205071  9271695 
##      372      374      377      383      384 
##  3160422  6761423  9271695  6761423 39076097
# mean squared error
y = mean((tree.pred-testing$Salary)^2)
y
## [1] 7.287e+13
# root mean squared error
z=sqrt(y)
z
## [1] 8536683

To calculate the best number of terminal nodes we do a 10-fold cross validation. We plot size versus cross-validation error rate and add a vertical line at the minimum error. From this we can see 9 is the ideal number of terminal nodes, because the cross-validation misclassification error is the lowest at that point.

set.seed(3142022)

#K=10-Fold cross validation
cv = cv.tree(tree.nba, K=10)

# the tree with smaller size
best.cv = min(cv$size[cv$dev == min(cv$dev)])
best.cv
## [1] 6
# Plot size vs. cross-validation error rate
 plot(cv$size , cv$dev, type="b", xlab = "Number of leaves, \'best\'",
      ylab = "CV Misclassification Error", col = "red", main="CV")
 abline(v=best.cv, lty=2)

# Add lines to identify complexity parameter
min.error = which.min(cv$dev) # Get minimum error index
abline(h = cv$dev[min.error],lty = 2)

We can now plot the pruned tree with 9 terminal nodes. The mean squared error (MSE) is lowest when the number of terminal nodes is 9.

# Prune tree
pt.cv = prune.tree(tree.nba, best=best.cv)

# # Plot pruned tree
plot(pt.cv)
text(pt.cv, pretty=0, col = "blue", cex = .5)
title("Pruned tree of size")

# Calculate the respective test error rate for the model

# Predict on test set
pred.pt.cv = predict(pt.cv, testing, type="vector")


 
# # examine misclassification errors on training set
# print('training errors')
# classes_test <- as.data.frame(testing) %>% pull(High)
# train_errors_topt <- table(class = classes_test, pred = pred.pt.cv)
# train_errors_topt/rowSums(train_errors_topt)

# test error rate for pt.cv for pt.cv is 0.27907

# The MSE is lower with 7 nodes (8047525). So best. ???

random forest

First, we will fit a random forest model on the “Salarygreater” variable whichis whether a player’s salary is greater than the average salary of the league.

rf_our_data = randomForest(salarygreater ~ PPG + APG + MPG + TPG + Minutes_percent + free_throws_attempted + Offensive_Box_Plus_Minus + Value_Over_Replacement, data = training_salarymean, mtry=3, ntree=500, importance=TRUE)
rf_our_data                        
## 
## Call:
##  randomForest(formula = salarygreater ~ PPG + APG + MPG + TPG +      Minutes_percent + free_throws_attempted + Offensive_Box_Plus_Minus +      Value_Over_Replacement, data = training_salarymean, mtry = 3,      ntree = 500, importance = TRUE) 
##                Type of random forest: classification
##                      Number of trees: 500
## No. of variables tried at each split: 3
## 
##         OOB estimate of  error rate: 18.12%
## Confusion matrix:
##     0  1 class.error
## 0 174 27      0.1343
## 1  29 79      0.2685

Next, we will check the error rate on the test dataset.

yhat.rf = predict (rf_our_data, newdata = testing_salarymean)
# Confusion matrix
rf.err = table(pred = yhat.rf, truth = testing_salarymean$salarygreater) 
test.rf.err = 1 - sum(diag(rf.err))/sum(rf.err) 
test.rf.err
## [1] 0.1948

The test set error rate is 0.1818.

Then we will get a plot with decreasing order of importance based on Model Accuracy and Gini value.

varImpPlot(rf_our_data, sort=T, main="Variable Importance for rf_our_data", n.var=5)

From the graphs we can see that among all the trees in the random forest, PPG is the most important variable in terms of Model Accuracy and Gini index.

PCA

dat <- our_data %>% select(-1)
summary(dat)
##      Salary              PPG            APG             MPG      
##  Min.   :  377645   Min.   : 1.9   Min.   : 0.20   Min.   : 9.3  
##  1st Qu.: 2401537   1st Qu.: 6.9   1st Qu.: 1.30   1st Qu.:19.3  
##  Median : 5565202   Median :10.0   Median : 2.05   Median :24.2  
##  Mean   :10044654   Mean   :11.4   Mean   : 2.65   Mean   :24.4  
##  3rd Qu.:13611280   3rd Qu.:14.2   3rd Qu.: 3.60   3rd Qu.:30.3  
##  Max.   :45780966   Max.   :32.0   Max.   :11.70   Max.   :37.6  
##       TPG       Minutes_percent free_throws_attempted Offensive_Box_Plus_Minus
##  Min.   :0.16   Min.   :19.5    Min.   :  2           Min.   :-9.30           
##  1st Qu.:0.83   1st Qu.:40.1    1st Qu.: 46           1st Qu.:-2.40           
##  Median :1.15   Median :50.5    Median : 77           Median :-0.80           
##  Mean   :1.37   Mean   :50.8    Mean   :111           Mean   :-0.61           
##  3rd Qu.:1.71   3rd Qu.:63.2    3rd Qu.:142           3rd Qu.: 1.10           
##  Max.   :4.80   Max.   :78.2    Max.   :581           Max.   : 7.70           
##  Value_Over_Replacement
##  Min.   :-0.700        
##  1st Qu.:-0.100        
##  Median : 0.100        
##  Mean   : 0.245        
##  3rd Qu.: 0.400        
##  Max.   : 2.400
# check variance of each variable in our_data
apply(dat, 2, var)
##                   Salary                      PPG                      APG 
##                1.090e+14                3.761e+01                3.856e+00 
##                      MPG                      TPG          Minutes_percent 
##                4.720e+01                6.469e-01                2.047e+02 
##    free_throws_attempted Offensive_Box_Plus_Minus   Value_Over_Replacement 
##                1.011e+04                7.724e+00                2.455e-01
# Principle component Analysis
pr.out = prcomp(dat, scale = TRUE)
# center is the mean and scale is the standard deviation
pr.out$center
##                   Salary                      PPG                      APG 
##                1.004e+07                1.138e+01                2.649e+00 
##                      MPG                      TPG          Minutes_percent 
##                2.439e+01                1.374e+00                5.080e+01 
##    free_throws_attempted Offensive_Box_Plus_Minus   Value_Over_Replacement 
##                1.107e+02               -6.104e-01                2.448e-01
pr.out$scale
##                   Salary                      PPG                      APG 
##                1.044e+07                6.133e+00                1.964e+00 
##                      MPG                      TPG          Minutes_percent 
##                6.870e+00                8.043e-01                1.431e+01 
##    free_throws_attempted Offensive_Box_Plus_Minus   Value_Over_Replacement 
##                1.005e+02                2.779e+00                4.955e-01
pr.out$rotation
##                             PC1      PC2      PC3     PC4      PC5      PC6
## Salary                   0.3297  0.07202 -0.07446 -0.2976 -0.88648 -0.02764
## PPG                      0.3759 -0.00594  0.12548  0.1969  0.03759 -0.41756
## APG                      0.3078 -0.32068 -0.58563 -0.3429  0.21256  0.04686
## MPG                      0.3501 -0.28423  0.47407 -0.1180  0.10630  0.17197
## TPG                      0.3414 -0.30575 -0.39608  0.1781  0.13916 -0.13719
## Minutes_percent          0.3500 -0.28440  0.47430 -0.1183  0.10586  0.17138
## free_throws_attempted    0.3273  0.12256 -0.08029  0.7933 -0.14532  0.15235
## Offensive_Box_Plus_Minus 0.3054  0.55795  0.08353 -0.2192  0.25609 -0.55227
## Value_Over_Replacement   0.3053  0.55719 -0.12548 -0.1346  0.19746  0.64552
##                               PC7      PC8        PC9
## Salary                    0.03918  0.06231 -0.0002591
## PPG                       0.30250 -0.73272 -0.0007387
## APG                      -0.48308 -0.24713 -0.0003454
## MPG                      -0.07152  0.11002 -0.7070501
## TPG                       0.57619  0.48083  0.0007399
## Minutes_percent          -0.07201  0.10863  0.7071624
## free_throws_attempted    -0.43503  0.09242  0.0001963
## Offensive_Box_Plus_Minus -0.23532  0.33827  0.0001065
## Value_Over_Replacement    0.29440 -0.14173  0.0003570
pr.out$x
##              PC1       PC2        PC3       PC4        PC5        PC6       PC7
##   [1,] -2.953288  0.751661 -0.7798325  0.913882 -0.4833563 -0.1116332 -0.056372
##   [2,]  0.309600 -0.010808  0.5639861 -0.143409 -0.6078346  0.2648778 -0.229813
##   [3,]  4.865673  0.847102 -0.5744453  0.509243 -0.2188013  1.0646140 -0.135499
##   [4,] -0.432518  0.179104  0.9000300 -0.336333  0.6863431 -0.3632763  0.199483
##   [5,] -1.084997 -0.656508 -0.1683950  0.271067  0.2137243 -0.2343125  0.365340
##   [6,] -0.319202  0.363541  0.5090096 -0.145746  0.5523045  0.0110519 -0.231867
##   [7,]  2.054258  1.138725  0.8151119  0.297599 -0.3690964  0.5505365 -0.020406
##   [8,]  1.574640  0.263231  0.9370147  0.539507 -0.7003640 -0.0324526 -0.292247
##   [9,]  1.968287  0.982038  0.9181402  0.308108 -0.4057285 -0.0191604 -0.274865
##  [10,]  1.317398  0.859840  0.0886806 -0.031238  0.5359803  0.4601145 -0.535582
##  [11,]  7.505250  2.002569 -1.3125248  2.032961 -0.8239888  0.9229956 -0.025665
##  [12,] -3.483751  0.817285 -1.2561830  0.447453 -0.3646207  0.0486265  0.214746
##  [13,] -0.680525 -0.249658  0.7531502  0.888354  0.1372372 -0.4493721 -0.412222
##  [14,]  0.169826 -2.024472 -0.3429429  0.610516  0.4565597 -0.4018189  0.002646
##  [15,]  1.690400 -0.307988  1.2042938 -0.383898  0.0043802  0.1875793  0.543786
##  [16,] -3.601610  1.005944 -1.0154955 -0.026471 -0.1699355 -0.0373684 -0.228698
##  [17,] -3.601610  1.005944 -1.0154955 -0.026471 -0.1699355 -0.0373684 -0.228698
##  [18,] -3.601610  1.005944 -1.0154955 -0.026471 -0.1699355 -0.0373684 -0.228698
##  [19,] -1.447356  0.183099 -0.5514384 -0.568790  0.0425339 -0.2499207 -0.425789
##  [20,] -0.560278 -0.318315 -0.8363249 -0.384608  0.3039290 -0.5440202 -0.066706
##  [21,] -1.883128 -0.454282  0.6875750 -0.105904 -0.1928822  0.1771457 -0.225015
##  [22,]  0.937953 -0.289167  1.1880570  0.545656 -0.2235609  0.0651434  0.105962
##  [23,] -0.352764 -0.782899  0.8563722  0.739578 -0.6240770 -0.7204108  0.126888
##  [24,]  2.666132 -0.216172 -1.1773657 -0.179331  1.3486967  0.2207202  0.265267
##  [25,]  2.111587 -1.084040 -0.2843413 -1.218861  0.2149036  0.0038869  0.123051
##  [26,] -1.644680  1.526023 -0.2558312 -0.065110 -0.1285507 -0.9149749 -0.066263
##  [27,] -1.249860  0.229178  0.3433749 -0.164448  0.4452733 -0.1548056 -0.042011
##  [28,]  2.858061 -0.260421  1.2016302  0.089394 -0.2165031  0.3090979 -0.598204
##  [29,]  1.977418 -1.427244  1.1103791  1.389283  0.1899247  0.1476421 -0.383458
##  [30,]  1.003893 -1.050995  0.6178310 -0.188701 -0.2622628 -0.0629579 -0.113759
##  [31,]  0.259189  1.047300  0.7204578 -1.055017  0.2312240  0.6816541 -0.145647
##  [32,]  0.259189  1.047300  0.7204578 -1.055017  0.2312240  0.6816541 -0.145647
##  [33,] -1.215212  0.648178 -0.2792036  0.101482  0.4365584  0.2728986  0.255898
##  [34,]  0.370613 -1.942230  0.9098712  1.293792  0.3342347 -0.2063500  0.295397
##  [35,]  6.837138  1.479901  0.0357255  1.493397 -0.4774521 -0.3174471  0.309568
##  [36,]  2.026378  0.257661  1.2821402 -0.636236  0.4297843 -0.3993889  0.563409
##  [37,] -1.482911  0.181037 -0.5790445  0.030886  0.3527050 -0.0532795  0.089204
##  [38,] -1.482911  0.181037 -0.5790445  0.030886  0.3527050 -0.0532795  0.089204
##  [39,] -0.696967  0.535443  0.0716701 -0.760113 -0.4189073  0.0943748 -0.052626
##  [40,] -0.011340  0.458248  1.0870831  0.412754  0.5644587  0.0525214 -0.335158
##  [41,] -1.771538  0.719502  0.2241635 -0.008451 -0.2252094  0.1209644 -0.436634
##  [42,] -0.029406 -0.582831  1.3037661 -0.484299  0.3968752  0.0498835  0.099810
##  [43,] -2.943165  1.100143 -0.6487924  0.273106 -0.3113573  0.0090769 -0.089617
##  [44,] -1.848512 -0.131409 -0.0006516  0.556673  0.0735863  0.1729048 -0.147598
##  [45,] -1.848512 -0.131409 -0.0006516  0.556673  0.0735863  0.1729048 -0.147598
##  [46,] -2.035687  0.172960 -0.5759886  0.029703  0.1757689 -0.4325313  0.053057
##  [47,]  1.275714 -0.764037  0.2561806  0.163991 -0.5465808 -0.0704356 -0.713295
##  [48,] -3.940006  0.297755 -0.9166755  0.203840 -0.5318452  0.1311536  0.221404
##  [49,]  4.486253 -1.118905 -0.0766391  1.774298 -1.3244204 -0.5962812  0.094048
##  [50,]  0.706703  2.249898  0.6667628  0.421049  0.5540206  0.2054851 -0.205261
##  [51,] -1.762463 -0.646165  0.1910153 -0.432666  0.2441274  0.2039293  0.123576
##  [52,] -1.441643  1.074348 -0.1197138 -0.042529  0.5430817 -0.9204062  0.287144
##  [53,]  0.190430 -0.966946  0.7790761  0.314602  0.3338269  0.0376647  0.169866
##  [54,]  1.328835  1.009464  1.5242656 -0.374327  1.0119065  0.8372755 -0.221676
##  [55,]  3.873520  0.102030  0.0734174 -0.749241  0.3890861  0.1345593 -0.142477
##  [56,]  0.531004 -1.964102  0.8156030  1.212536 -0.6723259 -0.6072240 -0.063699
##  [57,] -1.288333 -0.163394  0.3166984  0.463542 -0.1206446  0.0102708 -0.341750
##  [58,]  4.772327  1.194066  0.4358528  0.176338  0.2190131 -0.0317844  0.897842
##  [59,] -0.670382  1.776360  0.7075490  0.289826  0.7342286 -1.2720608 -0.424530
##  [60,] -1.154467  0.287073  0.6385131 -0.413535  0.4365576  0.3176499  0.129187
##  [61,]  0.228678  0.189644  0.0410189  0.249406  0.8749546 -0.3349843 -0.686081
##  [62,] -0.411025 -0.610248  1.4757229 -0.403831 -0.1711770  0.4866548 -0.043982
##  [63,] -2.241727  1.228053 -0.5095227 -0.008216  0.0283179 -0.2107668 -0.143555
##  [64,]  0.116466  0.364504  0.5227379 -0.018244  0.0705573 -0.0728886 -0.195618
##  [65,]  5.272191  0.412130 -0.7119962  0.547289 -1.0318960  0.3536473 -1.374080
##  [66,] -0.066615 -0.408023  0.9047218 -0.098928 -0.4358730  0.3606917 -0.239822
##  [67,] -1.015021 -0.318182 -0.5461832 -0.151341  0.3414398  0.3368029 -0.548570
##  [68,]  1.668608  1.243559  1.3214801  0.560583 -0.3422618  0.3813595  0.001955
##  [69,] -0.230287 -0.274700  0.1389640  0.247887  0.1567235 -0.0907369  0.125420
##  [70,] -0.319797 -0.300080  0.7221991 -0.069786  0.1784626 -0.0983076  0.310266
##  [71,] -3.414006  0.670008 -0.7911057 -0.143253 -0.4638380  0.1414177 -0.008815
##  [72,] -0.492298 -2.123454 -0.6550788  0.222837  0.4026639  0.3796164  0.304755
##  [73,] -0.877227  0.116896 -0.5873084 -0.261118 -0.0177321  0.3839027 -0.005047
##  [74,] -0.832571  0.477892  0.7093168  0.195963  0.3558303  0.0710840 -0.460878
##  [75,] -3.266940 -0.626049  0.1661644  0.040824 -0.2527991 -0.0209640 -0.198243
##  [76,]  1.868248  1.415605  0.2401022 -0.085407  0.5742967 -0.1875219  0.534291
##  [77,]  2.001528  1.230954  1.0383480  0.267870 -0.7287763  0.0678946  0.265330
##  [78,]  2.957724  0.647067 -0.6538268 -1.087782  0.3674352  0.0512557 -0.296520
##  [79,] -1.155369  1.091227  0.6588595 -0.630180  0.3238863  0.1382224 -0.232907
##  [80,] -1.018230  0.072148 -0.5610482  0.082417  0.6690816  0.0708446  0.490309
##  [81,] -1.018230  0.072148 -0.5610482  0.082417  0.6690816  0.0708446  0.490309
##  [82,] -1.018230  0.072148 -0.5610482  0.082417  0.6690816  0.0708446  0.490309
##  [83,] -1.018230  0.072148 -0.5610482  0.082417  0.6690816  0.0708446  0.490309
##  [84,] -0.116883 -0.900320  1.5030340 -0.564003 -0.3840014  0.7601244 -0.096874
##  [85,] -2.033467  0.550224  0.1247593 -0.285873 -0.0657901 -0.0619430  0.136689
##  [86,] -0.038813  0.198495  0.7888033 -0.491245  0.1192292  0.3935296 -0.098991
##  [87,] -2.950674 -0.097958 -0.5022170  0.396402 -0.7908033 -0.1040829  0.222775
##  [88,]  0.440228  0.011468  0.7906585 -0.513300  0.4644296 -0.0724950 -0.163658
##  [89,]  7.879428  2.538307 -0.9398243  0.218841 -0.7224181  0.3504241  0.972148
##  [90,]  4.315416  1.812426  0.3716963 -0.525901 -0.8311999  0.4582897  0.766772
##  [91,] -2.605461  0.453469 -0.5740997 -0.005075 -0.2094014 -0.4341249  0.282277
##  [92,] -1.304252 -0.361073  0.0827708  0.024725  0.1410810 -0.3968455  0.407000
##  [93,]  5.146761  0.797155 -0.5399447  0.921201 -0.1243691  0.5779791 -1.503365
##  [94,] -0.295271 -0.358135 -0.0541634  0.547004  0.2069679 -0.0250537  0.106916
##  [95,] -0.861042 -0.130526  0.3615112  0.206095  0.1190137 -0.0968121  0.525848
##  [96,] -1.525025  1.286871 -0.3809111 -0.221103  0.3365926 -0.4799360  0.198259
##  [97,]  0.104331 -0.297899  0.3330587 -0.413629  0.7056700  0.1950084 -0.055047
##  [98,] -0.228305 -1.711123  1.1200036  1.170578  0.1819644 -0.2942603 -0.120108
##  [99,] -1.264341  0.310348  0.1737359 -0.082916  0.4565951 -0.1214416 -0.179141
## [100,]  1.483729 -1.027095 -0.1269905  0.801480  1.1491031 -0.4124839  1.471763
## [101,] -0.144615 -0.097377  0.1767026  0.291116  0.7285378 -0.1427864  0.953549
## [102,]  5.811846  0.650119 -0.7571121 -0.470384 -0.8319470 -0.7406717  0.954069
## [103,]  1.484981 -2.150385  0.7567168  1.781412 -0.2962873 -0.7749458 -0.251997
## [104,] -1.042810  0.946277  0.4978255 -0.491162  0.6290845 -0.5664560 -0.163280
## [105,]  6.392896  2.689399 -0.3387548  2.512910 -0.5241544  0.2450835  0.495917
## [106,] -2.712056  0.376555 -0.7050061  0.906152 -0.3404250  0.2584694  0.129317
## [107,] -1.815726  1.864934 -0.3599870 -0.089887 -0.4189736  0.1334494 -0.007862
## [108,] -0.123785 -0.371093  1.7050701 -0.664318  0.6484200  0.3543495 -0.189177
## [109,] -2.245423 -1.318806 -0.5404482  0.287107 -0.2015252  0.6848926 -0.030195
## [110,]  2.124708 -0.398775  0.3002915 -0.269846 -0.0291918 -0.6979085  0.374129
## [111,]  0.809815 -0.025944  0.7625716 -1.411157 -0.1551094 -0.3073964  0.041218
## [112,]  5.564963 -0.566693 -0.7831757  1.150014 -0.2910280 -0.2425714 -0.773073
## [113,] -2.852764  1.251710 -0.7224783  0.186391 -0.0472290 -0.0676784  0.284676
## [114,] -1.837057  0.786544  0.0520090  0.406117  0.1354552 -0.1471220  0.411921
## [115,]  0.015266  0.226215  0.5570025  0.437456 -1.3128004 -0.5100578 -0.454359
## [116,]  2.377495 -2.114686 -0.4400513 -0.053723  1.1334164 -0.5345100  0.174754
## [117,] -0.879659  0.371336  0.2209953  0.329257  0.0256644 -0.3081322  0.056437
## [118,]  5.554676  0.861533 -0.5616707 -0.649419 -0.6828444 -0.3931580  0.847023
## [119,] -2.165834  0.062767  0.4364824  0.028586 -0.3112258  0.4393841 -0.111465
## [120,]  4.086373 -0.135969 -0.3742159  0.399809  1.9057537 -0.2395327  0.362654
## [121,]  3.303799  1.723017  0.6192817  1.059370 -1.7756502  1.2890934 -0.066766
## [122,]  1.907187 -0.734667 -0.3927675 -0.407400  0.2008559 -0.1975190  0.654336
## [123,]  0.123231  0.389572  0.4274651 -0.896774 -0.3255863  0.0461316  0.177548
## [124,]  1.821091  0.297199  0.5145450 -0.327374 -0.0748552 -0.5074490  0.535741
## [125,]  1.342297 -1.179838 -0.0078119 -0.131382  0.2875803 -0.1761237 -0.873409
## [126,]  3.952723  1.010571  0.8142954  1.156472 -0.0004322  0.3080204 -0.017022
## [127,] -0.382796 -0.221608  1.0029732 -0.616983 -0.0307409  0.2895241  0.088637
## [128,]  2.159640 -2.735234 -1.9179179 -1.473717 -0.2735565  0.9324486 -0.399845
## [129,] -1.471705  0.716174  0.1252744 -0.106674 -0.2341400 -0.5699474  0.045381
## [130,] -2.936041  1.096557 -0.3783516  0.040491 -0.0644588 -0.0792918  0.106584
## [131,] -0.247843  0.080654  0.9942319  0.529862  0.2231945  0.0911754 -0.576023
## [132,] -3.784240  0.298759 -0.8162991  0.243581 -0.6434493  0.1615184  0.074505
## [133,]  1.290220 -1.334877  0.4286652 -1.234452 -1.6539707 -0.1310954 -0.080071
## [134,]  1.290220 -1.334877  0.4286652 -1.234452 -1.6539707 -0.1310954 -0.080071
## [135,] -0.295624 -0.116228 -0.2929440 -0.865027 -2.1999899 -0.4325701  0.087590
## [136,] -0.295624 -0.116228 -0.2929440 -0.865027 -2.1999899 -0.4325701  0.087590
## [137,]  0.347852 -0.758947  1.5692443  0.677218  0.2574913 -0.0885009 -0.263270
## [138,]  1.548368  0.231292 -0.0237878 -1.247490  1.6457305  0.1606443 -0.290380
## [139,] -0.911420 -1.629832  0.0169260  0.345147  0.2357923  0.2266530  0.350436
## [140,]  1.119485  0.426763  1.0723134  0.131302 -0.9246976 -0.4286360 -0.352966
## [141,]  7.699888  0.135518 -2.3387574 -1.568150 -0.0604209  0.3191595  0.058425
## [142,]  6.854845 -1.406714 -2.0215375 -1.183388 -0.6153794 -1.2950483 -0.680148
## [143,]  7.413753 -0.386611 -2.2061149 -1.448080 -0.2412616 -0.3720046 -0.255601
## [144,] -1.502602 -0.506607  0.7368210 -0.202851  0.0071518  0.4909050 -0.012482
## [145,]  0.676333  1.772709  0.2676858  1.137223  0.0075962  0.2617919 -0.205505
## [146,] -0.257278 -0.943532  1.4782903 -0.938672 -1.1145189  0.0778575 -0.183741
## [147,] -0.479001 -0.705567  0.3088424 -0.599135 -1.1770720 -0.2685264  0.027863
## [148,]  1.117355  0.685789  1.4068599 -1.010475 -0.1018996  0.0424875  0.055229
## [149,]  3.645150  1.005442  0.4862139 -0.601045 -1.2203425  0.3016623  0.181019
## [150,]  0.073645 -0.291745  0.8041975 -0.420229 -0.1369521  0.3457353 -0.224912
## [151,] -2.133389  0.724904 -0.2108262  0.741742 -0.4673542 -0.0919270 -0.118581
## [152,] -0.749915 -3.849744 -1.5762738 -0.007381  0.0738932  0.8921767  1.134616
## [153,]  3.631216  0.304052  0.4484510 -0.576800 -0.6117612  0.1122552  0.216595
## [154,]  0.710932 -1.344094  0.5311363  0.256883  0.6844133 -0.3309781  0.025469
## [155,]  1.758426 -1.593960  0.9920033 -0.517436 -0.7118551 -0.5202416 -0.037983
## [156,] -0.255553  0.067282  0.5633036 -0.856810  0.7808248 -0.3053622 -0.305606
## [157,] -0.255553  0.067282  0.5633036 -0.856810  0.7808248 -0.3053622 -0.305606
## [158,] -2.233807 -0.166485  0.4047655 -0.039242 -0.0461225  0.3462726 -0.220562
## [159,] -2.388889 -0.912918 -0.4122584  0.599304 -0.4779860 -0.4313603 -0.261997
## [160,]  3.672074  0.275615 -0.4697970 -1.366778 -0.3053188  0.3570593  0.046753
## [161,]  0.255703  0.444974  1.3809806 -0.468669  0.5818171  0.6741497 -0.159630
## [162,]  0.630135 -0.164622  1.0369895  0.528634 -0.0933898  0.0788694 -0.114125
## [163,] -3.185523 -1.487895  0.0415273  0.246894 -0.5522538  0.0216380  0.058393
## [164,]  1.300776  0.713708  0.3928716 -1.733123 -0.7580108 -0.0056171  0.154846
## [165,] -0.783562 -0.427678 -0.8659351  0.505515 -0.3633977  0.2622022  0.170491
## [166,] -1.883165 -0.634260 -0.6426362  1.931308 -0.5131622 -0.0556577  0.018189
## [167,]  0.504298 -0.462375  0.8042638 -0.666898  0.8936421  0.4509713 -0.231046
## [168,]  1.009847  0.768969  1.0635404 -0.437195  0.7491104  0.0118542  0.309541
## [169,] -0.304826  0.790559  0.3061742 -0.514119  0.1485037 -0.2282339  0.195850
## [170,] -1.637898 -0.129486 -0.2472599 -0.327305  0.3109407  0.5921189  0.001381
## [171,]  1.832929  0.869243 -0.3865811 -1.161459  0.7544855  0.2751490 -0.050713
## [172,]  5.030854  0.816011 -0.0066077  0.366756 -0.2957116  0.0449856 -0.015981
## [173,]  5.504098  1.015368 -0.0562749 -0.902809 -0.2689484 -0.4851965  0.136416
## [174,] -2.437328 -0.797740 -0.1107409  0.865821 -0.5437423  0.6394697  0.586049
## [175,]  0.341552 -0.969517 -0.2068616  1.510830  0.1463796 -0.2197959  0.271412
## [176,] -0.430239 -0.109504 -0.1536443 -0.288263 -0.1166964 -0.1397586 -0.248571
## [177,]  6.985448  1.492159 -1.8379610 -1.063852 -0.1152550  0.7846357  1.074537
## [178,] -3.599838  1.425953 -0.8828720  0.089933 -0.2567224 -0.3632170 -0.062155
## [179,] -0.645714  1.035790  0.6941864 -0.433199  0.5296029  0.1202970 -0.067080
## [180,] -2.078673  0.428410 -0.4930817 -0.186081  0.1702833  0.2038268  0.197959
## [181,] -0.859029 -0.351843  0.2280910 -0.188046  0.4980109  0.3017056  0.264887
## [182,]  0.305099 -0.049699  0.9800658  0.883998  0.5599567  0.1609531 -0.433923
## [183,] -2.386289  0.359308 -0.5633799  0.151173  0.1311022  0.2622286  0.037136
## [184,] -2.386289  0.359308 -0.5633799  0.151173  0.1311022  0.2622286  0.037136
## [185,] -2.386289  0.359308 -0.5633799  0.151173  0.1311022  0.2622286  0.037136
## [186,] -2.386289  0.359308 -0.5633799  0.151173  0.1311022  0.2622286  0.037136
## [187,] -2.386289  0.359308 -0.5633799  0.151173  0.1311022  0.2622286  0.037136
## [188,] -1.564470  0.005718  0.6855819  0.132123 -0.6599333  0.1818534 -0.304220
## [189,] -2.728902 -1.401345 -0.1181598  0.479991 -0.4717354  1.2058383  0.600967
## [190,] -1.482151  0.478417 -0.9570825 -0.680147  0.0250159  0.0294079 -0.590988
## [191,] -0.475029  0.648808 -0.2178004 -0.076084 -0.0869585  0.3549123  0.418596
## [192,] -0.475029  0.648808 -0.2178004 -0.076084 -0.0869585  0.3549123  0.418596
## [193,] -1.612881 -0.684798 -0.1624951 -0.217665 -0.2823380  0.0664978 -0.194681
## [194,] -1.612881 -0.684798 -0.1624951 -0.217665 -0.2823380  0.0664978 -0.194681
## [195,]  0.005848 -1.895036 -0.6514544 -0.610022  0.3728839 -0.1274234 -0.177286
## [196,]  0.005848 -1.895036 -0.6514544 -0.610022  0.3728839 -0.1274234 -0.177286
## [197,] -1.988128  1.491431 -0.5110934 -0.171756  0.3288157 -0.2656309 -0.355521
## [198,] -1.433229  0.396076 -0.0386879 -0.528078 -0.6034908 -0.3567486 -0.032877
## [199,] -0.874997  0.101770  1.1156362 -0.704046 -0.0329436  0.1567138 -0.307637
## [200,] -2.976230  0.897636 -0.6210735 -0.038337 -0.1368085  0.0844449 -0.053921
## [201,] -1.888164 -0.278519 -0.0471194  0.437263 -0.3025024 -0.1292529 -0.047473
## [202,]  0.627654 -0.271425  0.7323323 -0.134704 -0.0809676 -0.1612697  0.365247
## [203,] -0.502017  1.607181  0.3182705 -0.512219  0.0409878 -0.3057879 -0.218738
## [204,]  5.613586  0.735502 -0.2848545  0.657028  0.9741101 -0.0663426  1.040207
## [205,] -2.880076  0.763164 -0.4591126  0.133045 -0.3496117 -0.0879797  0.137727
## [206,] -2.044385  0.600833  0.0947501 -0.135966  0.1930055 -0.0005504 -0.185726
## [207,] -2.315246 -1.029688 -1.4182288  0.500424 -0.1344859  0.7408551 -0.163450
## [208,] -2.009311  0.912149 -0.4217723  0.630058 -0.1680175  0.0876655  0.070556
## [209,] -2.754588 -0.449080 -0.4990738  1.113903 -0.7575141  0.7102646  0.324117
## [210,] -1.811520  1.273512 -0.3676734  0.488057 -0.0021581 -0.2700232 -0.081850
## [211,]  5.579246  1.811379 -0.0057241 -0.463765 -0.8413388  0.4055638 -0.046156
## [212,]  2.663881 -0.957701  0.2920849 -0.283578  0.1760513 -0.6944226 -0.015417
## [213,] -2.278387  0.075736 -0.5694007 -0.043261 -0.1397176 -0.0528430 -0.334439
## [214,]  7.879401  2.025631 -1.0571663  0.617771 -0.2489545  0.4756578 -0.489697
## [215,] -2.606279  1.798645 -0.3251243 -0.115024  0.1092699 -0.6798814 -0.304838
## [216,] -1.926287  0.514141 -0.1826601 -0.372911 -0.0139071  0.1459758 -0.409972
## [217,]  0.083325  0.394842  1.1963066  0.289076 -0.4826517 -0.0307964 -0.067806
## [218,] -1.405959  0.454433  0.0241303  0.811643 -0.1883566 -0.4718041 -0.106864
## [219,]  0.403067 -0.394964  0.0014427 -0.897102 -1.8400999 -0.4066157  0.413132
## [220,]  3.851318 -1.186749 -0.6957525 -1.022268 -0.0948244  0.1154537 -0.196880
## [221,] -2.745516  1.081360  0.0019843 -0.209755 -0.0457567 -0.3184060 -0.228487
## [222,] -2.057763  0.080837 -0.0527213  0.407002 -0.0301810  0.0531761 -0.401368
## [223,]  0.273264  0.862011  0.9713428 -0.264370 -0.4011946 -0.5113864  0.186933
## [224,] -1.772413 -1.405378 -0.4145182  0.432362 -0.0257435  1.0804723  0.219344
## [225,] -1.816191 -1.026895  0.5989509  0.652207 -0.1560623  0.7459926  0.236392
## [226,] -2.547707  0.657987 -0.4562394  0.219311  0.0945767 -0.0845618 -0.231396
## [227,] -2.293932  0.679562 -0.5475705 -0.155757  0.2038790 -0.0125198 -0.185895
## [228,] -2.366621  1.482201  0.1228122  0.319540  0.0698969 -0.0144576 -0.471435
## [229,] -2.332136  0.393227  0.3061817 -0.101253  0.0395413  0.1528369 -0.227762
## [230,] -2.241368  0.398893 -0.6420547  0.238940 -0.0488004 -0.3724950 -0.278419
## [231,]  4.124060  1.550169  0.8566912 -1.594595 -0.1443743 -0.9539616 -0.411268
## [232,]  0.820558 -0.652022 -1.3153298 -1.240688  1.0297946  0.5975891 -0.268479
## [233,] -2.296368 -0.541533 -0.0766630  0.535606 -0.1960007  0.4210156  0.333623
## [234,] -2.056848 -1.002237  0.7526979  0.162524 -0.1185075  0.3469445 -0.024104
## [235,] -0.197539  0.495800  0.8018194 -0.078733 -0.4794184 -0.5171966 -0.083975
## [236,] -2.125675  0.238028 -0.7546346  0.426889 -0.2656150 -0.2389069  0.673488
## [237,] -1.756077 -0.153605 -0.9577525 -0.543852  0.4839295 -0.0159854 -0.493184
## [238,] -2.546589 -0.043645 -0.2086149  0.243677 -0.1764185 -0.2492160  0.296628
## [239,] -2.608137  0.013538  0.1134837  0.183931 -0.2076262 -0.2518026  0.335610
## [240,] -0.849103  0.408537 -0.4530935 -0.384246  0.0808733 -0.3101664  0.112849
## [241,] -2.585801  0.983723 -0.5958685  0.182508  0.0322622 -0.5243244  0.150030
## [242,]  4.673315  0.559013 -0.3979879 -0.549438 -0.7400440  0.2422885  0.231084
## [243,] -0.719612  1.280911 -0.0017869 -0.039352  0.6538044 -0.0660832 -0.173960
## [244,] -0.108875  0.715297  0.4040541 -0.436673  0.5757213 -0.1513205 -0.241554
## [245,] -0.049939 -0.344043 -0.3266944  0.893480  0.5620300 -0.3831332 -0.274189
## [246,]  4.776873 -0.044665 -0.2298625  0.728776 -0.4155246 -0.4201622  0.153648
## [247,] -0.851990  0.264536 -0.0894201  0.112332  0.5950923 -0.7935532  0.065816
## [248,] -3.060902 -0.218323 -0.8251303  0.108054 -0.3367067  0.3003852  0.353485
## [249,]  3.861852 -1.508193 -1.1280640  1.460123  0.9679461 -0.6069078 -1.068370
## [250,] -1.954978  0.094283  0.0418787  0.081126  0.0579700  0.0566874  0.100761
## [251,] -0.107879  0.417605  0.3629517 -0.714870  0.3521509 -0.0611199 -0.640508
## [252,]  2.015804 -0.884801 -0.0028481 -0.549242  0.2243519  0.4550017 -0.273220
## [253,]  3.484804 -0.979465  0.5232711 -0.690877 -0.9177152 -0.4890737  0.113562
## [254,] -1.652314  1.188442  0.2340582 -0.199048  0.2470345 -0.5304115  0.094643
## [255,] -2.340510  1.096771 -0.4160482  0.028092  0.1435136 -0.4657670  0.173795
## [256,]  0.654820 -0.572329  0.6354322 -0.880447  0.3805668  0.7744413  0.238260
## [257,] -3.306898  0.059875 -0.3226657  0.272476 -0.6102240  0.1647609  0.186189
## [258,] -1.179315  0.123234  0.0755999  0.041254  0.3852399  0.0345557 -0.347992
## [259,] -2.705885  0.170306 -0.2441814  0.126063 -0.2867976 -0.0737704  0.309874
## [260,] -3.430918  1.976721 -0.6562681 -0.186875 -0.1527573 -0.8106391 -0.243893
## [261,] -1.284909 -0.039475  0.5470942 -0.028621 -0.3694228  0.9591200  0.343817
## [262,] -1.816116  0.662154 -0.0530524  0.015598  0.2665714 -0.5235836 -0.177633
## [263,]  0.160255 -0.989205  0.8687094 -0.170742  0.5037272 -0.1804806  0.234506
## [264,] -1.404690  0.305816  0.6392309  0.039889 -0.0121299  0.3247084  0.014694
## [265,] -2.598243  0.830006  0.0834690 -0.138680  0.0369741 -0.1444684 -0.223766
## [266,] -1.249352 -0.593324  0.5395914 -0.323466  0.2573933  0.3510377 -0.161914
## [267,] -2.342557 -0.748990  0.1779223  0.722740 -0.5660173  0.2604220 -0.337738
## [268,] -3.220596  0.590230 -0.7615895  0.470721 -0.8295222  0.2690361  0.292585
## [269,] -0.487958 -2.456091  1.4310012  0.855195 -0.4441413  0.2814036 -0.524724
## [270,] -4.003510 -0.052933 -0.6825356  0.341643 -0.6858240  0.5118928  0.248634
## [271,]  2.081859 -1.647838  0.2762116 -0.168387  1.5089439 -0.5201011  0.588024
## [272,]  2.268662 -1.306551  0.3273051 -0.302499  1.6655889 -0.8579183  0.444085
## [273,]  1.793788 -2.173885  0.2817817  0.004286  1.2910199 -0.4825955  0.596192
## [274,] -0.104462 -0.128030  0.5488994 -0.674604 -0.0826686  0.2190207  0.309925
## [275,]  2.072369 -1.209419 -0.0226270  0.168653  0.4153345 -0.2218312  0.729864
## [276,]  0.264814 -0.275674  1.0035208 -0.936310  0.4495163  1.1542916  0.051487
## [277,] -4.107925  0.712891 -0.9338860  0.146673 -0.4601591  0.1484626  0.109487
## [278,] -4.107925  0.712891 -0.9338860  0.146673 -0.4601591  0.1484626  0.109487
## [279,] -0.200513 -0.501010  0.1585030 -0.097579  0.1238074 -0.1442980 -0.206076
## [280,]  0.428757 -1.050925  1.3944884  0.862240 -0.5580478 -0.0817785 -0.060116
## [281,] -1.691294  0.450044 -0.2976347  0.547430  0.1646498 -0.6583189  0.009969
## [282,]  4.266249  0.146583 -1.5839783 -1.567723 -0.0655325  0.7816706 -0.703874
## [283,] -1.170613  0.358753 -0.9644814 -0.436270  0.1766790 -0.1436498 -0.311361
## [284,] -0.745023 -1.130010 -0.2815391  0.421504  0.3665844 -0.3422099 -0.192747
## [285,]  0.870004 -0.138782 -0.3089984  0.252278  0.4667720  0.4877967 -0.104445
## [286,] -0.121879 -0.018468  0.5371764  0.084825  0.0240674  0.4873771 -0.108820
## [287,] -1.528586 -2.463814 -0.3003481  0.539079 -0.1342687  0.5000708  1.020029
## [288,] -0.968783  0.632439 -0.1580839  0.505125  0.3782176 -0.5617331 -0.169253
## [289,]  1.103866  0.228333  1.6926951  0.378700  0.6845548 -0.5320992  0.262800
## [290,] -0.561292  1.708225  0.3014788 -0.206124  0.6063907 -0.3456096  0.162486
## [291,] -1.955703  0.508464 -0.3996374  0.511222 -0.9404431  0.1593417 -0.248400
## [292,]  1.561451 -0.368275  0.9781802  0.526392 -0.2782541 -0.4710255  0.530656
## [293,]  1.541193 -0.686376  1.6050908 -0.198171 -0.1228345 -0.1356875  0.360970
## [294,] -0.523869 -0.325448  0.0719374 -0.504843 -0.5857502 -0.1985140  0.123265
## [295,] -0.666719 -0.586432  0.0328659 -0.402287 -0.7055375  0.0598168  0.233336
## [296,] -0.374339 -0.052386  0.0706550 -0.595125 -0.4721810 -0.2272026  0.114948
## [297,] -1.715488  0.433971 -0.1185966 -0.149447  0.3080271 -0.1405125 -0.082668
## [298,] -0.273163  1.760410 -0.2513245  0.555063  0.7072662 -0.2442174 -0.432370
## [299,]  6.170864  0.463381 -0.4819769  1.343532  0.8467849  1.1061847  0.157188
## [300,] -0.653739 -1.291286  1.1771359  0.324495  0.0409249 -0.0486843  0.165742
## [301,] -0.927358  1.057757 -0.0133499  0.975553  0.3035626 -0.1829618 -0.068380
## [302,]  0.273095 -1.182458  0.9359836 -0.038764 -0.2293855 -0.1188768 -0.255527
## [303,] -1.780012 -0.391410 -0.0177160 -0.294101  0.2424081 -0.0869249  0.122934
## [304,] -1.049790 -0.921928  0.6899777 -0.556507  0.4700938  0.1471830 -0.175838
## [305,]  0.291249 -0.994798  1.4574164 -0.355346 -0.5645686 -0.2563396 -0.058555
## [306,] -0.640938  0.613348  1.2922556 -0.282478  0.6965576  0.7608151  0.340821
## [307,] -0.602411 -0.307809 -0.1040275  0.580895  0.5143647  0.0111659  0.352335
## [308,] -2.349564 -0.462094 -1.6012931 -0.396256  0.1740866 -0.0358144  0.124086
## [309,] -2.349564 -0.462094 -1.6012931 -0.396256  0.1740866 -0.0358144  0.124086
## [310,] -0.824787 -1.580064 -1.8420249 -0.594266  0.7328186 -0.0696001  0.127663
## [311,] -0.824787 -1.580064 -1.8420249 -0.594266  0.7328186 -0.0696001  0.127663
## [312,]  0.586683 -0.039567 -0.8483577 -0.704049  0.1826472 -0.6394386  0.378030
## [313,]  0.569014 -0.071720 -0.8110094 -0.708435  0.1796509 -0.8492092  0.284743
## [314,]  0.320588 -0.525463 -0.8367765 -0.547154 -0.0168480 -0.6416762  0.369264
## [315,]  0.901830 -0.138287 -0.0401487 -0.724027  0.1802383 -0.3550494 -0.167885
## [316,]  0.884161 -0.170441 -0.0028005 -0.728413  0.1772420 -0.5648199 -0.261172
## [317,]  0.635736 -0.624184 -0.0285676 -0.567132 -0.0192569 -0.3572869 -0.176651
## [318,]  0.678317 -0.947659  0.7555147  0.359474 -0.2703603 -0.3422361  0.074475
## [319,]  3.470006  0.489339  0.6724365 -0.229873  0.4749846  0.1897500 -0.033029
## [320,]  0.335516 -2.091618 -1.0838584 -0.238294 -0.7720796  0.1330129 -1.174715
## [321,]  3.099370 -0.410585 -0.9814581 -0.698365 -0.6822844 -0.8307317  0.198260
## [322,]  5.294600 -0.129766 -0.8670823  0.533648  0.9374444  0.8716091  0.178249
## [323,]  3.880593 -0.808946  0.3877739  2.083413  1.0336827 -0.3483118 -0.300975
## [324,] -1.518510 -0.544823  0.5210202  0.244978 -0.0721182 -0.2401584 -0.288373
## [325,]  3.978729 -0.840698  0.4537768  0.580535 -1.1392336 -0.0149132 -0.267722
## [326,]  4.488976  0.024836 -1.3911835 -0.251945 -0.4572725  1.1739241 -0.160708
## [327,] -1.811753  0.942539 -0.1459433 -0.095160  0.1211383 -0.6649724 -0.272611
## [328,]  2.122351 -1.167183 -0.0729257 -0.413168  0.3519600  0.2923830 -0.620516
## [329,] -1.819960 -1.061955 -0.8927076 -0.257805  0.2562324  0.2939453  0.036710
## [330,] -2.164910 -1.692300 -1.0292373  0.006916 -0.0448468  1.1594786  0.409408
## [331,] -1.622168 -0.700593 -0.8386087 -0.399806  0.4220918 -0.0637435 -0.115695
## [332,] -1.379097 -0.477213 -0.6035431 -0.613336  0.2640444  0.1484962 -0.481408
## [333,] -2.125189  0.220271  0.4501214 -0.483338  0.1702169  0.1028871 -0.229231
## [334,] -3.438029  0.295766 -0.6729714  0.525643 -0.4597378  0.0338876  0.081966
## [335,] -1.671070 -0.195031  0.3155146  0.515145 -0.0975000  0.0674746  0.073133
## [336,] -2.586459  2.034504 -0.2763600 -0.317035  0.2520633 -0.7458399 -0.256592
## [337,]  0.178445 -0.670924  0.6989586  0.493674  0.6913005  0.4719266 -0.165640
## [338,]  5.492139  1.029082  0.3018049  0.658562 -0.0773644  0.1346933  0.345559
## [339,] -0.956617 -0.946956  0.7436116 -0.295876  0.1509229  0.3408319 -0.175433
## [340,] -0.493129  0.508272  0.4992793 -0.338855  0.1140867  0.2332450  0.121429
## [341,] -0.415294  0.365636  0.4789213 -0.538727  0.2068533  0.1720246  0.269852
## [342,] -0.965025 -0.296190  0.4655267  0.375781 -0.4819414 -0.1556429 -0.209002
## [343,] -2.259109  0.468804  0.1745921 -0.360460  0.0557066  0.9963214  0.180712
## [344,] -3.190217  1.367215 -0.7461265 -0.038958 -0.4616026 -0.1793502  0.066597
## [345,] -1.488291 -0.168561 -0.4133997 -0.407335  0.5129750  0.1599302 -0.079365
## [346,]  4.778022 -0.303462 -0.1354680  0.768293 -0.7601570 -0.9284934  0.344067
## [347,]  0.649687  0.194681  1.6809954 -0.595923 -0.3359391 -0.3196794 -0.082588
## [348,]  0.645394  0.092101  1.9337476 -0.932749 -0.2534484 -0.4117458  0.134815
## [349,] -1.511983 -1.948023  1.3222628 -0.338891 -0.3716573  0.5409625 -0.111806
## [350,] -3.099476 -0.770363  0.4104011 -0.234846 -0.8531878  0.2298899 -0.249875
## [351,]  1.135866  0.287801  1.1997912  0.016353 -0.5052273  0.8350558  0.540390
## [352,] -1.759167  0.847005 -0.3205514  0.310595 -0.0378630  0.3675741 -0.075380
## [353,]  4.045789  0.161624  0.2598587 -0.867364  0.6693845  0.6991544 -0.460750
## [354,] -2.283499  1.020701 -0.0690014 -0.091198 -0.1288645  0.3786805 -0.078124
## [355,] -3.587996 -0.527275 -0.8143328  0.398030 -0.5613993  0.0707475  0.158440
## [356,] -2.614375 -0.336288  0.1735923  0.125815 -0.1975930  0.2535871 -0.131344
## [357,] -2.274542  0.773097 -0.5943422  0.169968  0.1154848 -0.2365667  0.128948
## [358,]  2.081689 -0.996801  0.2125162  0.003811  0.7977707 -0.4867089 -0.142512
## [359,]  2.081689 -0.996801  0.2125162  0.003811  0.7977707 -0.4867089 -0.142512
## [360,]  4.723665 -1.132905 -1.3499480 -0.663433 -1.6300833 -0.7384906  0.364119
## [361,] -2.629320 -0.507376 -0.9299819  0.259972 -0.0433633  0.2208467 -0.163761
## [362,] -2.629320 -0.507376 -0.9299819  0.259972 -0.0433633  0.2208467 -0.163761
## [363,] -1.782503 -1.183909 -0.9632244  0.100642  0.2939383  0.1461567  0.128620
## [364,] -1.782503 -1.183909 -0.9632244  0.100642  0.2939383  0.1461567  0.128620
## [365,]  0.656739 -1.142642  0.6253354  0.580487  0.5596197  0.2277059  0.284626
## [366,] -2.980883  0.795979 -0.3257934 -0.012774 -0.1253495 -0.0392094 -0.100019
## [367,]  6.730916 -3.355550 -2.9121091  0.549185 -1.3780910 -0.5562868 -0.858867
## [368,]  1.101216 -2.172517  0.0808510  0.324815  0.5839289 -0.5959408 -0.304685
## [369,]  1.141122 -0.269879  0.6116867 -0.435643 -0.0834784 -0.3020936 -0.340326
## [370,] -1.899213  1.140411 -0.4929582  0.458137  0.1429668 -0.4079832  0.392287
## [371,]  2.471921 -0.728664  1.0420777  0.491013 -1.7188530 -0.1522732 -0.041244
## [372,] -2.537211 -0.222187 -0.1515458  0.272916 -0.2247596  0.1066645  0.071892
## [373,] -1.175596  0.033300 -0.1593544 -0.066087  0.4688888  0.1884078  0.041527
## [374,] -0.238368 -0.187209 -0.5464793  0.257399  0.4077260 -0.5844965 -0.200054
## [375,] -0.599216 -0.252747 -0.7147162 -0.264954  0.4888227 -0.6030844  0.094317
## [376,]  5.330960  1.535853  0.0510709  2.845875  1.0355393  0.4017313 -0.196998
## [377,] -0.685059 -1.334354  0.9052844  0.595043 -0.3320181 -0.0025049 -0.105632
## [378,] -2.153411  0.763428 -0.4141845 -0.149049  0.2377114 -0.2865888  0.222723
## [379,] -1.228940 -0.616680  0.1453529  0.661659 -0.6061591 -0.2710394  0.749815
## [380,]  2.687035  0.850265  1.1797256  0.439458  0.4674151 -0.3495988  0.572136
## [381,]  1.353033  0.450943 -0.1362410 -1.101828  1.0351217  0.9290292 -0.397056
## [382,]  0.571103  0.909300 -0.1422548 -1.184987  0.8481176  0.6747937  0.211453
## [383,]  1.071581  0.213182 -0.8776539 -0.431252  0.2048406  0.0542266  0.176693
## [384,]  6.816582  0.164447 -2.2371852  2.223351  2.1507597  0.1038001 -0.934552
## [385,] -0.811231  0.769457 -0.0477005  0.323576  0.4874797 -0.1012274 -0.158879
## [386,] -0.305297  1.000864  0.1600812  0.570965  0.0521935  0.0879360 -0.214851
##               PC8        PC9
##   [1,]  0.0266966 -5.223e-04
##   [2,]  0.7817200 -4.641e-03
##   [3,]  0.2254060 -3.103e-03
##   [4,] -0.3507602  1.790e-03
##   [5,] -0.1268965 -1.244e-03
##   [6,] -0.0182690  1.372e-04
##   [7,]  0.5185849 -1.707e-04
##   [8,]  0.5685040 -6.413e-04
##   [9,]  0.7139439 -3.523e-04
##  [10,] -0.0066114 -3.616e-03
##  [11,] -0.1437614 -9.138e-04
##  [12,]  0.0411851 -5.378e-03
##  [13,] -0.3355296 -2.504e-03
##  [14,]  0.1245938 -2.610e-03
##  [15,]  0.0852572  1.712e-03
##  [16,] -0.2755101  1.884e-03
##  [17,] -0.2755101  1.884e-03
##  [18,] -0.2755101  1.884e-03
##  [19,]  0.1531449 -5.564e-03
##  [20,] -0.0592085  3.064e-03
##  [21,]  0.0542084 -2.235e-03
##  [22,]  0.1571742  2.875e-03
##  [23,] -0.0031581 -3.230e-03
##  [24,]  0.2117924  1.269e-03
##  [25,]  0.0594816 -2.276e-03
##  [26,]  0.3227826 -5.896e-03
##  [27,] -0.0490456  2.014e-03
##  [28,]  0.2140522 -5.731e-03
##  [29,] -0.0269883  6.385e-05
##  [30,]  0.2608745  1.018e-03
##  [31,]  0.2581679  1.349e-03
##  [32,]  0.2581679  1.349e-03
##  [33,]  0.2540549  2.733e-03
##  [34,]  0.4501993 -4.085e-03
##  [35,] -0.2454147 -3.302e-03
##  [36,] -0.2604182  3.311e-03
##  [37,]  0.4967798 -3.897e-03
##  [38,]  0.4967798 -3.897e-03
##  [39,]  0.2131702  1.231e-03
##  [40,] -0.0337802 -3.463e-03
##  [41,]  0.2142970  2.475e-03
##  [42,]  0.0007421 -1.560e-03
##  [43,] -0.2165874 -2.444e-03
##  [44,]  0.4668465  5.504e-03
##  [45,]  0.4668465  5.504e-03
##  [46,]  0.0717710 -5.125e-04
##  [47,]  0.2842459  6.242e-03
##  [48,] -0.0485547 -2.840e-04
##  [49,] -0.0791995 -1.065e-03
##  [50,] -0.0953282 -3.292e-04
##  [51,]  0.3407150 -4.816e-04
##  [52,]  0.4876480  3.683e-04
##  [53,]  0.0960299 -1.668e-03
##  [54,] -0.1540191 -1.834e-04
##  [55,] -0.5027433 -3.705e-03
##  [56,] -0.2556640  3.309e-04
##  [57,] -0.0592901  1.907e-03
##  [58,] -0.0031057 -2.645e-03
##  [59,]  1.0472337  1.575e-03
##  [60,]  0.0065826 -1.905e-04
##  [61,] -0.1286230 -4.127e-03
##  [62,] -0.2586179 -2.468e-04
##  [63,] -0.1228765 -1.519e-03
##  [64,] -0.1874196 -1.794e-03
##  [65,] -0.3468648 -5.063e-03
##  [66,]  0.1449741 -3.196e-03
##  [67,]  0.1223286  3.903e-03
##  [68,]  0.1391773  5.147e-03
##  [69,]  0.1933558  2.430e-03
##  [70,]  0.0658781  4.722e-03
##  [71,] -0.4182237  4.640e-03
##  [72,]  0.2171776 -1.812e-03
##  [73,]  0.2653260 -2.032e-03
##  [74,] -0.2285344 -1.708e-04
##  [75,]  0.0620611 -1.189e-03
##  [76,] -0.2396846  4.112e-03
##  [77,] -0.0836135  3.074e-03
##  [78,] -0.0969839 -2.260e-03
##  [79,]  0.2034277  1.755e-04
##  [80,] -0.0038669  6.242e-03
##  [81,] -0.0038669  6.242e-03
##  [82,] -0.0038669  6.242e-03
##  [83,] -0.0038669  6.242e-03
##  [84,]  0.2017828  6.818e-03
##  [85,] -0.0839862  1.416e-03
##  [86,]  0.0079597 -4.366e-03
##  [87,] -0.0281506 -1.590e-03
##  [88,] -0.0279014  5.397e-03
##  [89,] -0.5041606 -6.477e-03
##  [90,] -0.1840601 -3.992e-03
##  [91,] -0.0892844 -7.821e-04
##  [92,] -0.1811024  1.873e-04
##  [93,] -0.4413379 -1.801e-04
##  [94,] -0.0570508 -3.890e-03
##  [95,] -0.0044374 -1.851e-03
##  [96,]  0.2301773 -2.225e-04
##  [97,]  0.1228185  8.418e-04
##  [98,] -0.0520677 -3.675e-03
##  [99,]  0.1944951 -6.182e-04
## [100,]  0.5032693  1.183e-03
## [101,]  0.4322928 -2.213e-03
## [102,]  0.0402431 -2.436e-03
## [103,] -0.0522111  6.099e-03
## [104,]  0.0949792 -1.733e-03
## [105,]  0.1037270  2.155e-03
## [106,] -0.1407220 -3.296e-03
## [107,]  0.1981426 -3.560e-03
## [108,]  0.1583952  1.960e-03
## [109,] -0.7057852  2.304e-03
## [110,] -0.2281707 -6.369e-03
## [111,] -0.0086197  1.710e-03
## [112,] -0.2365121 -9.552e-04
## [113,]  0.0934810 -1.540e-03
## [114,] -0.2845716 -4.236e-03
## [115,] -0.1720993 -5.741e-03
## [116,]  0.2427594  7.701e-03
## [117,] -0.1317043  5.212e-03
## [118,]  0.4585343  5.503e-03
## [119,] -0.0102318 -1.727e-03
## [120,] -0.2164052  6.777e-04
## [121,]  0.5690208  2.962e-03
## [122,]  0.4846134  3.359e-03
## [123,]  0.1857803  2.140e-03
## [124,]  0.0103252  3.448e-03
## [125,] -0.3444709 -6.220e-03
## [126,] -0.1424659 -4.321e-04
## [127,]  0.1003481  3.413e-03
## [128,]  0.6705494 -5.334e-03
## [129,]  0.2939758 -4.713e-04
## [130,]  0.1152277  2.460e-03
## [131,] -0.0084118 -2.394e-03
## [132,] -0.1579915  2.024e-03
## [133,]  0.1670906 -1.012e-03
## [134,]  0.1670906 -1.012e-03
## [135,]  0.0441724  4.387e-03
## [136,]  0.0441724  4.387e-03
## [137,]  0.0521960  3.893e-03
## [138,] -0.0337427  9.909e-05
## [139,] -0.2905457  9.533e-06
## [140,] -0.3202720 -4.011e-03
## [141,]  0.0249370  3.362e-03
## [142,]  0.3481230  2.410e-03
## [143,]  0.1923073  3.010e-03
## [144,]  0.0179034  6.257e-03
## [145,] -0.0209437  1.534e-04
## [146,]  0.0736361  1.884e-03
## [147,]  0.0191840  5.558e-03
## [148,] -0.0743176  5.660e-03
## [149,] -0.1826370 -4.096e-04
## [150,]  0.2537370  6.720e-04
## [151,] -0.0937191 -2.282e-03
## [152,]  0.3581397 -1.399e-03
## [153,] -0.0524588 -1.418e-03
## [154,] -0.0575047 -1.062e-03
## [155,]  0.0290223 -3.303e-03
## [156,] -0.2704079  4.970e-03
## [157,] -0.2704079  4.970e-03
## [158,]  0.1772650  1.321e-03
## [159,] -0.0838374  2.629e-04
## [160,] -0.1718195  5.554e-04
## [161,]  0.0142784  4.156e-03
## [162,] -0.0796890 -1.525e-03
## [163,] -0.0781637 -7.829e-05
## [164,] -0.3356008  3.043e-03
## [165,]  0.0215148 -3.459e-03
## [166,]  0.4360195  3.482e-03
## [167,] -0.2007105  1.823e-03
## [168,] -0.0690995 -2.483e-03
## [169,]  0.0329042 -2.020e-03
## [170,]  0.2634808 -3.191e-03
## [171,]  0.2563243 -5.435e-03
## [172,] -0.1391631  2.617e-03
## [173,] -0.5710369  4.247e-03
## [174,] -0.8284998 -7.518e-03
## [175,]  0.3493629 -4.100e-03
## [176,] -0.2006944  3.738e-03
## [177,] -0.0689391  2.238e-03
## [178,] -0.1075539  5.679e-03
## [179,] -0.0095807 -4.770e-03
## [180,]  0.0345861 -2.315e-03
## [181,] -0.0006578 -1.717e-03
## [182,]  0.0046222  1.650e-03
## [183,]  0.1435219 -3.437e-03
## [184,]  0.1435219 -3.437e-03
## [185,]  0.1435219 -3.437e-03
## [186,]  0.1435219 -3.437e-03
## [187,]  0.1435219 -3.437e-03
## [188,]  0.1069462  2.212e-04
## [189,] -0.7456539  1.348e-03
## [190,] -0.2392777 -3.411e-03
## [191,]  0.5234107 -5.281e-03
## [192,]  0.5234107 -5.281e-03
## [193,]  0.0233517 -4.769e-03
## [194,]  0.0233517 -4.769e-03
## [195,] -0.3759763 -4.623e-04
## [196,] -0.3759763 -4.623e-04
## [197,] -0.0799459  1.417e-03
## [198,] -0.0422422 -2.198e-03
## [199,]  0.2872005  3.349e-03
## [200,] -0.1520574 -1.126e-03
## [201,] -0.3105331 -9.002e-04
## [202,]  0.3142195  5.785e-03
## [203,]  0.0177694 -3.866e-03
## [204,] -0.0457468 -4.660e-03
## [205,] -0.0394331  1.815e-03
## [206,] -0.0539002  1.081e-03
## [207,] -0.4975839 -3.140e-03
## [208,] -0.0176048  4.227e-03
## [209,] -0.5525292  3.815e-03
## [210,]  0.2014802  4.296e-03
## [211,] -0.5573426 -2.023e-03
## [212,] -0.3269609  2.555e-03
## [213,] -0.2849326  4.784e-03
## [214,] -0.3291448  1.736e-03
## [215,]  0.3383487  4.428e-03
## [216,]  0.2436402  8.615e-04
## [217,]  0.0826611 -3.264e-03
## [218,]  0.2424950 -4.425e-03
## [219,]  0.0951473  5.959e-04
## [220,]  0.1450262  3.691e-04
## [221,]  0.0318603 -5.308e-03
## [222,] -0.1782927 -4.007e-03
## [223,]  0.0830173  2.327e-03
## [224,] -0.5597296 -1.273e-03
## [225,] -0.5177496  1.059e-03
## [226,]  0.0462988  5.685e-03
## [227,]  0.2657968  2.234e-03
## [228,]  0.0070199  2.353e-03
## [229,]  0.1113831 -1.265e-03
## [230,] -0.3513056  5.534e-03
## [231,] -0.4197780  2.486e-03
## [232,]  0.0883251  2.227e-03
## [233,] -0.2689253  4.873e-03
## [234,] -0.0684838 -1.239e-04
## [235,] -0.2472493  2.268e-03
## [236,]  0.0463051  6.700e-03
## [237,]  0.0409144 -1.762e-03
## [238,] -0.1346670 -3.019e-04
## [239,] -0.2451618  1.612e-03
## [240,]  0.1156869  1.262e-03
## [241,]  0.1059060 -6.689e-03
## [242,]  0.0897585 -3.544e-03
## [243,]  0.1391358 -6.212e-03
## [244,]  0.0358790 -3.189e-03
## [245,] -0.0647551 -1.477e-03
## [246,] -0.4531101  8.874e-04
## [247,]  0.0148745  2.827e-03
## [248,] -0.2807104 -2.936e-04
## [249,]  0.4294635  1.407e-04
## [250,]  0.0795662 -1.971e-03
## [251,] -0.1370245 -6.489e-03
## [252,] -0.4281708  1.978e-03
## [253,] -0.2066186 -3.339e-03
## [254,] -0.1422815  2.972e-03
## [255,]  0.1132626 -1.681e-03
## [256,]  0.3528290  8.343e-04
## [257,] -0.2402542  4.066e-03
## [258,] -0.1286337  3.609e-03
## [259,] -0.2447200  2.899e-03
## [260,]  0.2133845 -1.842e-05
## [261,]  0.4016601 -4.993e-03
## [262,] -0.1730572 -5.768e-03
## [263,] -0.2974263 -3.028e-03
## [264,] -0.2968565  6.308e-04
## [265,]  0.1385895 -5.902e-03
## [266,] -0.0870764 -4.989e-03
## [267,]  0.0433942  2.500e-04
## [268,] -0.2266788  4.609e-03
## [269,]  0.2412584  5.027e-03
## [270,] -0.3260792 -8.526e-04
## [271,] -0.4848291 -4.363e-03
## [272,] -0.2779155 -4.297e-03
## [273,] -0.6101889 -4.564e-03
## [274,]  0.1667481 -1.802e-03
## [275,] -0.1318077  8.661e-04
## [276,]  0.4201882 -5.609e-03
## [277,] -0.1865087 -4.340e-04
## [278,] -0.1865087 -4.340e-04
## [279,]  0.1242539 -1.579e-03
## [280,] -0.2020124 -3.112e-03
## [281,]  0.0314241 -2.579e-03
## [282,] -0.3609990  4.171e-03
## [283,] -0.2597320 -4.031e-04
## [284,]  0.1116146 -3.195e-03
## [285,]  0.3837831 -6.394e-04
## [286,]  0.3501655  4.344e-03
## [287,]  0.1666345  4.757e-03
## [288,] -0.2613005 -1.100e-03
## [289,] -0.2923566  4.546e-03
## [290,] -0.0143256 -1.472e-03
## [291,]  0.0399351  5.554e-03
## [292,] -0.2425883  3.242e-03
## [293,] -0.0122631 -3.184e-03
## [294,]  0.0299290  9.417e-04
## [295,] -0.1282991  8.919e-04
## [296,]  0.0986946  1.044e-03
## [297,] -0.0628158 -1.203e-04
## [298,] -0.0552978  4.373e-03
## [299,]  0.0881475 -4.525e-03
## [300,]  0.1027071 -5.223e-03
## [301,] -0.0923368  5.143e-03
## [302,]  0.0903383  3.597e-03
## [303,]  0.0291851 -2.932e-03
## [304,] -0.0978448 -2.203e-03
## [305,]  0.0222940 -1.199e-03
## [306,]  0.2404537  1.295e-03
## [307,]  0.5941286 -1.406e-03
## [308,]  0.1634343 -1.971e-03
## [309,]  0.1634343 -1.971e-03
## [310,]  0.0862902  5.192e-03
## [311,]  0.0862902  5.192e-03
## [312,] -0.1303232 -5.113e-03
## [313,] -0.0530322 -5.170e-03
## [314,] -0.2313403 -5.307e-03
## [315,] -0.3781646 -2.142e-03
## [316,] -0.3008735 -2.199e-03
## [317,] -0.4791817 -2.336e-03
## [318,] -0.1450690 -2.254e-03
## [319,] -0.2746182  6.560e-03
## [320,] -0.0417200 -4.294e-03
## [321,]  0.0626905 -3.866e-03
## [322,]  0.3525760  1.971e-03
## [323,] -0.1239661 -6.101e-03
## [324,] -0.1420414 -1.246e-03
## [325,] -0.1081825  8.348e-04
## [326,]  0.5887214 -3.401e-03
## [327,]  0.0821986 -2.366e-03
## [328,]  0.1877908  3.305e-03
## [329,] -0.3779060  3.000e-03
## [330,] -0.8568530  2.930e-03
## [331,] -0.1588210  3.069e-03
## [332,] -0.1995524 -2.836e-03
## [333,]  0.0642400  1.919e-03
## [334,] -0.1286060 -2.377e-03
## [335,]  0.0984376  9.706e-04
## [336,]  0.0317020  4.146e-04
## [337,]  0.0657812 -1.018e-03
## [338,] -0.2423031 -3.319e-03
## [339,]  0.1475958  1.384e-03
## [340,]  0.0793433 -6.747e-03
## [341,]  0.0528848  6.411e-03
## [342,]  0.5074356  1.087e-03
## [343,] -0.0548626 -3.005e-03
## [344,] -0.0592964  3.730e-03
## [345,]  0.2246121 -1.734e-03
## [346,]  0.2830544 -3.151e-04
## [347,] -0.2147372 -3.829e-03
## [348,] -0.3839175 -3.216e-03
## [349,]  0.5558916 -4.603e-03
## [350,]  0.0998687  1.972e-03
## [351,]  0.2405724 -3.295e-03
## [352,]  0.2080050  1.229e-03
## [353,] -0.4973597  3.120e-03
## [354,] -0.1616521  3.973e-03
## [355,] -0.4077948 -2.635e-04
## [356,] -0.2860352  4.496e-03
## [357,] -0.0822778  2.351e-03
## [358,] -0.3891238  2.348e-03
## [359,] -0.3891238  2.348e-03
## [360,]  0.4593353  1.001e-03
## [361,] -0.1427598  3.101e-03
## [362,] -0.1427598  3.101e-03
## [363,] -0.0603506 -6.093e-03
## [364,] -0.0603506 -6.093e-03
## [365,]  0.2879312  3.776e-03
## [366,] -0.0664125 -5.654e-03
## [367,]  0.6134913  3.841e-03
## [368,]  0.1001084  5.044e-03
## [369,] -0.2914155  6.517e-03
## [370,]  0.1873517 -3.004e-03
## [371,] -0.0402518  9.241e-04
## [372,]  0.1920255 -5.259e-03
## [373,]  0.0381409  2.828e-04
## [374,] -0.0334797 -6.237e-03
## [375,]  0.2164100 -2.726e-03
## [376,] -0.2370179  3.289e-03
## [377,]  0.4007151  4.012e-03
## [378,]  0.4214119  6.543e-03
## [379,] -0.0160833  5.760e-03
## [380,]  0.0107557  6.034e-03
## [381,] -0.0504981  3.930e-03
## [382,] -0.0031970 -1.841e-03
## [383,]  0.1747146 -7.129e-04
## [384,]  0.2779788  6.754e-03
## [385,]  0.1146139  3.212e-03
## [386,]  0.4101175  2.719e-03
# plot 
biplot(pr.out, scale = 0)

# number of prinicipal components needed
pr.out$sdev
## [1] 2.511785 0.970954 0.772104 0.654397 0.583431 0.427380 0.359670 0.267184
## [9] 0.003455
pr.var = pr.out$sdev^2
pr.var
## [1] 6.309e+00 9.428e-01 5.961e-01 4.282e-01 3.404e-01 1.827e-01 1.294e-01
## [8] 7.139e-02 1.194e-05
pve = pr.var/sum(pr.var)
pve
## [1] 7.010e-01 1.048e-01 6.624e-02 4.758e-02 3.782e-02 2.029e-02 1.437e-02
## [8] 7.932e-03 1.326e-06
# plot 
plot(pve, xlab = "Principal Component", ylab = "Proportion of Variance Explained", ylim = c(0,1), type = "b")

plot(cumsum(pve), xlab=  "Principal Component", ylab = "Cumulative Proportion of Variance Explained", ylim=c(0,1), type= "b")

pr.out$rotation[,1]
##                   Salary                      PPG                      APG 
##                   0.3297                   0.3759                   0.3078 
##                      MPG                      TPG          Minutes_percent 
##                   0.3501                   0.3414                   0.3500 
##    free_throws_attempted Offensive_Box_Plus_Minus   Value_Over_Replacement 
##                   0.3273                   0.3054                   0.3053
# we will consider the first two PCAs because they are the largest 2 values. 

The first principal component explains 70% of the variance in data, the next principal component explains 10.48%. Having greater PPG is the best “indicator” for variance explained in data.

Cluster

scar  = scale(dat, center=TRUE, scale=TRUE)
km = kmeans(scar, centers=2)
km
## K-means clustering with 2 clusters of sizes 300, 86
## 
## Cluster means:
##    Salary     PPG     APG     MPG     TPG Minutes_percent free_throws_attempted
## 1 -0.3654 -0.4245 -0.3441 -0.3593 -0.3974         -0.3593               -0.3733
## 2  1.2747  1.4807  1.2004  1.2535  1.3861          1.2535                1.3023
##   Offensive_Box_Plus_Minus Value_Over_Replacement
## 1                  -0.3262                -0.3421
## 2                   1.1380                 1.1933
## 
## Clustering vector:
##   [1] 1 1 2 1 1 1 2 2 2 1 2 1 1 1 2 1 1 1 1 1 1 1 1 2 2 1 1 2 2 1 1 1 1 1 2 2 1
##  [38] 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 1 1 2 1 1 1 1 1 1 2 1 1 2 1 1 1 1 1 1
##  [75] 1 2 2 2 1 1 1 1 1 1 1 1 1 1 2 2 1 1 2 1 1 1 1 1 1 2 1 2 2 1 2 1 1 1 1 2 1
## [112] 2 1 1 1 2 1 2 1 2 2 2 1 2 1 2 1 2 1 1 1 1 1 1 1 1 1 2 1 1 2 2 2 1 1 1 1 1
## [149] 2 1 1 1 2 1 2 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 2 2 2 1 1 1 2 1 1 1 1 1 1 1 1
## [186] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 2 2 1 2 1 1 1 1 1 2 1 1
## [223] 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 2 1 1 1 2 1 1 2 1 1 2 2 1 1 1 1 1 1
## [260] 1 1 1 1 1 1 1 1 1 1 1 2 2 2 1 2 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 2 2 1 1 1
## [297] 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 2 2 1 2 2 1 2 1 1 1 1 1
## [334] 1 1 1 1 2 1 1 1 1 1 1 1 2 1 1 1 1 1 1 2 1 1 1 1 2 2 2 1 1 1 1 1 1 2 1 1 1
## [371] 2 1 1 1 1 2 1 1 1 2 1 1 1 2 1 1
## 
## Within cluster sum of squares by cluster:
## [1] 1141.7  692.6
##  (between_SS / total_SS =  47.1 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"
fviz_cluster(km, data= dat, gemo= "point", stand = FALSE, frame.type = "norm")

# Dim 1 is more influential. cluster 2 is skewed right

# centering the winner data so we can find out the loadings for the first two PCs

# data_svd <- svd(scar)
# data_loadings <- data_svd$v[,1:2]
# data_pc <- as.matrix(scar) %*% data_loadings 
# colnames(data_loadings) <- colnames(data_pc) <- paste('PC', 1:2, sep = '')
# 
# Player <- our_data$Player %>% factor()
# 
# data_pc %>%
#   as.data.frame() %>%
#   #bind_cols((dplyr:: select(data, state, county, candidate))) %>%
#   ggplot(aes(x = PC1, y = PC2)) +
#   geom_point(alpha = 0.5, aes(color = Player)) +
#   theme_bw()

Function to predict

# salary_prediction <- function(m, PPG, APG, MPG, TPG, Minutes_percent, free_throws_attempted, Offensive_Box_Plus_Minus, Value_Over_Replacement){
#   
#   pre_new <- predict(m, data.frame(PPG = points, APG = assists, MPG = minutes, TPG = turnovers, Minutes_percent = minutes_p, free_throws_attempted = free_throws, Offensive_Box_Plus_Minus = offense_plus_minus, Value_Over_Replacement = value))
#   
#   msg <- paste("PPG:", points, ",APG:", assists, ",MPG:", minutes, ",TPG:", turnovers, ",Minutes_percent:", minutes_p, ",free_throws_attempted:", free_throws, ",Offensive_Box_Plus_Minus:", offense_plus_minus, ",Value_Over_Replacement", value ," ==> Expected Salary: $", format(round(pre_new), big.mark = ","), sep = "")
#   
#   print(msg)
# }
# 
# 
# model <- lm(Salary ~ PPG+ APG + MPG + TPG + Minutes_percent + free_throws_attempted + Offensive_Box_Plus_Minus + Value_Over_Replacement, data = our_data)
# salary_prediction(model, 16, APG = 4, 30, 3, 75, 400, 4.5, 3)


# user.name <- readline(prompt = "Please type your name ")
#   print(paste("Users name is", user.name))
#   
# 
# install.packages("nlme")
# library(nlme)
# 
# salary_prediction <- function(m, PPG, APG){
#   
#   pre_new <- predict(m, c(PPG = points, APG = assists))
#   
#   msg <- paste("PPG:", points, ",APG", assists, " :Expected Salary: $", as.character(pre_new))
#   print(msg)
# }
# 
# model <- lm(Salary ~ PPG+APG, data = our_data)
# salary_prediction(model, 16,3)

Conclusion